package com.gxc.authcore.system.util;

import com.gxc.webcore.controller.IAccessCheck;
import com.gxc.webcore.util.ContextUtil;
import com.gxc.webcore.util.StringUtil;

public class CheckUtil {

	/**
	 * 判断是否需要权限检查
	 * 
	 * @param url
	 * @return
	 */
	public static boolean needCheck(String url) {
		boolean needCheck = false;
		for (String item : ParamUtil.get("access_control_url").split(";")) {
			if (StringUtil.isTrimEmpty(item)) {
				continue;
			} else {
				item = item.trim();
			}
			while (item.startsWith("/")) {
				item = item.substring(1);
			}
			if (url.startsWith(item)) {
				needCheck = true;
				break;
			}
		}
		return needCheck;
	}

	/**
	 * 检查登陆状态
	 * 
	 * @param url
	 * @return
	 */
	public static boolean checkLogin(String url) {
		if (CurrentUtil.getAuthInfo() == null) {
			return false;
		}
		return true;
	}

	/**
	 * 检查权限
	 * 
	 * @param url
	 * @return
	 */
	public static boolean checkAccess(String url) {
		// TODO 访问权限验证开关
		if (!ParamUtil.get("access_control").equals("1")) {
			return true;
		}
		// TODO 访问权限验证
		if (needCheck(url)) {
			for (String item : ParamUtil.get("access_check_beans").split(";")) {
				if (StringUtil.isTrimEmpty(item)) {
					continue;
				} else {
					item = item.trim();
				}
				try {
					IAccessCheck accessService = (IAccessCheck) ContextUtil
							.getBean(item);
					if (!accessService.check(url)) {
						return false;
					}
				} catch (Exception e) {
					e.printStackTrace();
					return false;
				}
			}
		}
		return true;
	}
}
